package com.rc.saas.mini.controller.rc;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rc.saas.base.MinipBaseController;
import com.rc.saas.mini.charge.ChargeService;
import com.rc.saas.mini.common.base.DataTResponse;
import com.rc.saas.mini.common.base.TResponse;
import com.rc.saas.mini.mapper.rc.RcNoticeNotificationMapper;
import com.rc.saas.mini.minip.vo.LoginInfoVo;
import com.rc.saas.mini.model.order.OrderRefundApply;
import com.rc.saas.mini.model.rc.RcNoticeNotification;
import com.rc.saas.mini.model.tenant.TenantNotice;
import com.rc.saas.mini.service.invoice.InvoiceApplyService;
import com.rc.saas.mini.service.order.OrderInfoService;
import com.rc.saas.mini.service.order.OrderRefundApplyService;
import com.rc.saas.mini.service.rc.RcNoticeNotificationService;
import com.rc.saas.mini.service.rc.RcNoticeNotificationServiceImpl;
import com.rc.saas.mini.service.tenant.TenantNoticeService;
import com.rc.saas.mini.vo.base.RcPageVo;
import com.rc.saas.mini.vo.invoice.InvoiceApplyWithIdVo;
import com.rc.saas.mini.vo.order.OrderInfoVo;
import com.rc.saas.mini.vo.rc.RcNoticeNotificationVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description
 * @Author
 * @Date: 2022-07-23
 */

@Api(tags = "27.用户-消息列表")
@Controller
@RequestMapping("/rcNoticeNotification")
public class RcNoticeNotificationController extends MinipBaseController {

    private static Logger logger = LogManager.getLogger(RcNoticeNotificationController.class);

    @Autowired
    private RcNoticeNotificationService rcNoticeNotificationService;

    @Autowired
    private OrderInfoService orderInfoService;

    @Autowired
    private TenantNoticeService tenantNoticeService;
    @Autowired
    private InvoiceApplyService invoiceApplyService;
    @Autowired
    private OrderRefundApplyService orderRefundApplyService;
    @Autowired
    private ChargeService chargeService;


    ///**
    // *
    // */
    //@ApiOperation(value = "用户-消息通知表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    //@RequestMapping(value = "/saveOrUpdateRcNoticeNotification", method = RequestMethod.POST)
    //@ResponseBody
    //private TResponse saveOrUpdateRcNoticeNotification(HttpServletRequest request, RcNoticeNotification rcNoticeNotification) {
    //    LoginInfoVo loginInfo = readLoginInfo(request);
    //    if (rcNoticeNotification.getNoticeNotificationId() == null) {
    //        rcNoticeNotification.setCreatedTime(new Date());
    //        rcNoticeNotification.setCreatedBy(loginInfo.getMemberId() + "");
    //        rcNoticeNotificationService.insertSelective(rcNoticeNotification);
    //    } else {
    //        rcNoticeNotification.setLastUpdatedTime(new Date());
    //        rcNoticeNotification.setLastUpdatedBy(loginInfo.getMemberId() + "");
    //        rcNoticeNotificationService.updateByPrimaryKeySelective(rcNoticeNotification);
    //    }
    //    return TResponse.SUCCESS();
    //}

    /**
     *
     */
    @ApiOperation(value = "单个查看用户-消息(已读操作)", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @RequestMapping(value = "/selectByNoticeNotificationId", method = RequestMethod.POST)
    @ResponseBody
    private DataTResponse<RcNoticeNotificationVo> selectByNoticeNotificationId(HttpServletRequest request, Long noticeNotificationId) {
        LoginInfoVo loginInfo = readLoginInfo(request);
        if (noticeNotificationId != null) {
            RcNoticeNotificationVo rcNoticeNotificationVo = new RcNoticeNotificationVo();
            RcNoticeNotification rcNoticeNotification = rcNoticeNotificationService.selectByPrimaryKey(noticeNotificationId);

            if(rcNoticeNotification==null){
                return DataTResponse.FAIL("查无数据");
            }
            if ("2".equals(rcNoticeNotification.getMsgType())) {
                //订单
                OrderInfoVo orderInfoVo = orderInfoService.selectOrderById(Long.valueOf(rcNoticeNotification.getMsgId()));
                List<Long> ids = new ArrayList<>();
                ids.add(orderInfoVo.getOrderId());
                List<InvoiceApplyWithIdVo> invoiceApplyList = invoiceApplyService.listByOrderIds(ids);
                List<OrderRefundApply> orderRefundApplyList = orderRefundApplyService.listByOrderIds(ids);
                //剩余充电时间 todo

                orderInfoVo.setSurplusTime(chargeService.getRemainChargingTime(orderInfoVo.getOrderNum()));
                orderInfoVo.setOrderRefundApply(orderRefundApplyList.stream().filter(data -> orderInfoVo.getOrderId().equals(data.getOrderId())).findFirst().orElseGet(() -> null));
                orderInfoVo.setInvoiceApplyWithIdVo(invoiceApplyList.stream().filter(data -> orderInfoVo.getOrderId().equals(data.getOrderId())).findFirst().orElseGet(() -> null));
                rcNoticeNotificationVo.setOrderInfoVo(orderInfoVo);
            } else if ("0".equals(rcNoticeNotification.getMsgType()) || "1".equals(rcNoticeNotification.getMsgType())) {
                TenantNotice tenantNotice = tenantNoticeService.selectByPrimaryKey(Long.valueOf(rcNoticeNotification.getMsgId()));
                rcNoticeNotificationVo.setTenantNotice(tenantNotice);
            }

            rcNoticeNotificationVo.setRcNoticeNotification(rcNoticeNotification);
            rcNoticeNotification.setIsRead("1");
            rcNoticeNotification.setLastUpdatedTime(new Date());
            rcNoticeNotificationService.updateByPrimaryKeySelective(rcNoticeNotification);

            return DataTResponse.SUCCESS_DATA(rcNoticeNotificationVo);
        }
        return DataTResponse.FAIL("参数不正确");
    }


    /**
     *
     */
    @ApiOperation(value = "单个查看用户-消息(已读操作)-订单", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @RequestMapping(value = "/selectByNoticeNotificationIdOrder", method = RequestMethod.POST)
    @ResponseBody
    private DataTResponse<OrderInfoVo> selectByNoticeNotificationIdOrder(HttpServletRequest request, Long noticeNotificationId) {
        LoginInfoVo loginInfo = readLoginInfo(request);
        if (noticeNotificationId != null) {
            RcNoticeNotificationVo rcNoticeNotificationVo = new RcNoticeNotificationVo();
            RcNoticeNotification rcNoticeNotification = rcNoticeNotificationService.selectByPrimaryKey(noticeNotificationId);
            if(rcNoticeNotification==null){
                return DataTResponse.FAIL("查无数据");
            }
            if ("2".equals(rcNoticeNotification.getMsgType())) {
                //订单
                OrderInfoVo orderInfoVo = orderInfoService.selectOrderById(Long.valueOf(rcNoticeNotification.getMsgId()));
                List<Long> ids = new ArrayList<>();
                ids.add(orderInfoVo.getOrderId());
                List<InvoiceApplyWithIdVo> invoiceApplyList = invoiceApplyService.listByOrderIds(ids);
                List<OrderRefundApply> orderRefundApplyList = orderRefundApplyService.listByOrderIds(ids);
                //剩余充电时间 todo

                orderInfoVo.setSurplusTime(chargeService.getRemainChargingTime(orderInfoVo.getOrderNum()));
                orderInfoVo.setOrderRefundApply(orderRefundApplyList.stream().filter(data -> orderInfoVo.getOrderId().equals(data.getOrderId())).findFirst().orElseGet(() -> null));
                orderInfoVo.setInvoiceApplyWithIdVo(invoiceApplyList.stream().filter(data -> orderInfoVo.getOrderId().equals(data.getOrderId())).findFirst().orElseGet(() -> null));
                rcNoticeNotification.setIsRead("1");
                rcNoticeNotification.setLastUpdatedTime(new Date());
                rcNoticeNotificationService.updateByPrimaryKeySelective(rcNoticeNotification);
                return DataTResponse.SUCCESS_DATA(orderInfoVo);
            }

        }
        return DataTResponse.FAIL("参数不正确");
    }


    /**
     *
     */
    @ApiOperation(value = "单个查看用户-消息(已读操作)-通知/活动", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @RequestMapping(value = "/selectByNoticeNotificationIdNotice", method = RequestMethod.POST)
    @ResponseBody
    private DataTResponse<TenantNotice> selectByNoticeNotificationIdNotice(HttpServletRequest request, Long noticeNotificationId) {
        LoginInfoVo loginInfo = readLoginInfo(request);
        if (noticeNotificationId != null) {
            RcNoticeNotificationVo rcNoticeNotificationVo = new RcNoticeNotificationVo();
            RcNoticeNotification rcNoticeNotification = rcNoticeNotificationService.selectByPrimaryKey(noticeNotificationId);
            if(rcNoticeNotification==null){
                return DataTResponse.FAIL("查无数据");
            }
            if ("0".equals(rcNoticeNotification.getMsgType()) || "1".equals(rcNoticeNotification.getMsgType())) {
                TenantNotice tenantNotice = tenantNoticeService.selectByPrimaryKey(Long.valueOf(rcNoticeNotification.getMsgId()));
                rcNoticeNotification.setIsRead("1");
                rcNoticeNotification.setLastUpdatedTime(new Date());
                rcNoticeNotificationService.updateByPrimaryKeySelective(rcNoticeNotification);
                return DataTResponse.SUCCESS_DATA(tenantNotice);
            }
        }
        return DataTResponse.FAIL("参数不正确");
    }

    /**
     * 分页查询消息通知
     */
    @ApiOperation(value = "分页查询消息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @RequestMapping(value = "/listPageByRcNoticeNotification", method = RequestMethod.POST)
    @ResponseBody
    private DataTResponse<PageInfo<RcNoticeNotification>> listPageByRcNoticeNotification(HttpServletRequest request, RcNoticeNotification rcNoticeNotification, RcPageVo lkPageVo) {
        //Page
        LoginInfoVo loginInfo = readLoginInfo(request);
        PageHelper.startPage(lkPageVo.getPageNo(), lkPageVo.getPageSize());
        List<RcNoticeNotification> list = rcNoticeNotificationService.listByMemberId(loginInfo.getMemberId());
        PageInfo<RcNoticeNotification> pageInfo = new PageInfo<>(list);
        return DataTResponse.SUCCESS_DATA(pageInfo);
    }
}
